<?php

class Ext_Video_Fetch
{
    protected $_url;
    
    protected $_messager;
    const URL_FORMAT_ERROR       = 'urlFormatError';
    const HOST_ADAPTER_NOT_FOUND = 'hostAdapterNotFound';
    protected $_messageTemplates = array(
        self::URL_FORMAT_ERROR           => "The video url farmat error.",
        self::HOST_ADAPTER_NOT_FOUND     => "The host adapter %host% not found.",
    );
    
    public function __construct($url)
    {
        $this->_url = $url;

        $this->_messager = new Ext_Messager($this->_messageTemplates);
    }
    
    public function fetch()
    {
        $result = array(
            'code'      => Ext_Video_Fetch_Result::FAILURE,
            'datas'     => array(),
            'messages'  => array(),
        );

        $urlArray = parse_url($this->_url);
        $host = isset($urlArray['host']) ? trim(strtolower($urlArray['host'])) : '';
        if(!$host){
            $result['messages'][] = $this->_messager->addMessage(self::URL_FORMAT_ERROR);
            return new Ext_Video_Fetch_Result($result['code'], $result['datas'], $result['messages']);
        }

        $config = Ext_Config::getInstance()->getConfig("attachment");
        $attachmentConfig = $config->toArray();
        if(!is_array($attachmentConfig)){
            $attachmentConfig = array();
        }
        if(!is_array($attachmentConfig['video'])){
            $attachmentConfig['video'] = array();
        }
        $hostAdapter = '';
        foreach($attachmentConfig['video'] as $hostKey=>$hostVal)
        {
            if(!isset($hostVal['host'])){
                continue;
            }
            $hostDomain = $hostVal['host'];
            if(preg_match("/".$hostDomain."$/si", $host)){
                $hostAdapter = $hostKey;
                break;
            }
        }
        if(!$hostAdapter){
            $result['messages'][] = $this->_messager->addMessage(self::HOST_ADAPTER_NOT_FOUND, array('host'=>$host));
            return new Ext_Video_Fetch_Result($result['code'], $result['datas'], $result['messages']);
        }
        
        $classname = "Ext_Video_Fetch_Download_Adapter_".ucfirst($hostAdapter);
        if(!class_exists( $classname )){
            $result['messages'][] = $this->_messager->addMessage(self::HOST_ADAPTER_NOT_FOUND, array('host'=>$host));
            return new Ext_Video_Fetch_Result($result['code'], $result['datas'], $result['messages']);
        }
        $adapter = new $classname($this->_url);
        $resulter = $adapter->download();
        if(!$resulter->isValid()){
            $result['messages'] = $resulter->getMessages();
            return new Ext_Video_Fetch_Result($result['code'], $result['datas'], $result['messages']);
        }
        $html = $resulter->getHtml();
        
        $classname = "Ext_Video_Fetch_Parse_Adapter_".ucfirst($hostAdapter);
        if(!class_exists( $classname )){
            $result['messages'][] = $this->_messager->addMessage(self::HOST_ADAPTER_NOT_FOUND, array('host'=>$host));
            return new Ext_Video_Fetch_Result($result['code'], $result['datas'], $result['messages']);
        }
        $adapter = new $classname($this->_url, $html);
        $adapter->parse();
        if(!$adapter->isValid()){
            $result['messages'] = $adapter->getMessages();
            return new Ext_Video_Fetch_Result($result['code'], $result['datas'], $result['messages']);
        }
        $datas = $adapter->getDatas();

        $result['code']  = Ext_Video_Fetch_Result::SUCCESS;
        $result['datas'] = $datas;
        return new Ext_Video_Fetch_Result($result['code'], $result['datas'], $result['messages']);
    }
    
}
